package com.example.administrator.commonlibrary.bese;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.administrator.commonlibrary.contract.fragment.five.FiveFragmentContract;
import com.example.administrator.commonlibrary.contract.fragment.four.FourFragmentContract;
import com.example.administrator.commonlibrary.contract.fragment.one.OneFragmentContract;
import com.example.administrator.commonlibrary.contract.fragment.three.ThreeFragmentContract;
import com.example.administrator.commonlibrary.contract.fragment.two.TwoFragmentContract;
import com.example.administrator.commonlibrary.dagger.component.DaggerOverallSituationComponent;
import com.example.administrator.commonlibrary.dagger.component.OverallSituationComponent;
import com.example.administrator.commonlibrary.dagger.module.OverallSituationModule;
import com.example.administrator.commonlibrary.thirdparty.LeakCanaryConfig;
import com.example.administrator.commonlibrary.utils.system.ClassUtils;
import com.example.administrator.commonlibrary.networkframework.DebugConfig;
import com.example.administrator.commonlibrary.welcomepage.MyApplicationThread;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BaseAppliction extends Application {
    private List<BaseFragment> fragments;    //Fragment集合
    private static BaseAppliction baseAppliction;
    private List<String> fPath = new ArrayList<>();
    private static OverallSituationComponent build;

    @Override
    public void onCreate() {
        super.onCreate();
        baseAppliction = this;
        //创建全局单例
        build = DaggerOverallSituationComponent.builder().overallSituationModule(new OverallSituationModule(this)).build();
        fPath.add("com.example.administrator.fivepagelibrary.entrance");
        fPath.add("com.example.administrator.fourpagelibrary.entrance");
        fPath.add("com.example.administrator.onepagelibrary.entrance");
        fPath.add("com.example.administrator.threelibrary.entrance");
        fPath.add("com.example.administrator.twopagelibrary.entrance");
        //设置app  测试或者打包模式
        DebugConfig.init(DebugConfig.MODE_DEBUG);
        //初始化数据的线程
        Intent intent = new Intent(this, MyApplicationThread.class);
        startService(intent);
        //查找出 指定包下面的主界面的所有Fragment
        getNewsFragment();
        if (DebugConfig.AROUTER){//路由的初始化
            ARouter.openLog();
            ARouter.openDebug();
            LeakCanaryConfig.init(BaseAppliction.getBaseAppliction());//检查内存泄漏
        }
        ARouter.init(BaseAppliction.getBaseAppliction());

    }

    public static BaseAppliction getBaseAppliction() {
        return baseAppliction;
    }

    public List<BaseFragment> getNewsFragment() {
        if (fragments == null) {
            fragments = new ArrayList<>();
            BaseFragment[] fragmenArray = new BaseFragment[5];
            //  List<IViewDelegate> viewDelegates = ClassUtils.getObjectsWithInterface(this, IViewDelegate.class, "com.example.administrator");
            //这样指定绝对路径查找的快   不要再这个文件夹中创建太多的类
            List<IViewDelegate> viewDelegates = ClassUtils.getObjectsWithInterface(this, IViewDelegate.class, fPath);
            if (viewDelegates != null && !viewDelegates.isEmpty()) {
                for (IViewDelegate view : viewDelegates) {
                    if (view.getFragment() instanceof OneFragmentContract.View) {
                        fragmenArray[0] = view.getFragment();
                    } else if (view.getFragment() instanceof TwoFragmentContract.View) {
                        fragmenArray[1] = view.getFragment();
                    } else if (view.getFragment() instanceof ThreeFragmentContract.View) {
                        fragmenArray[2] = view.getFragment();
                    } else if (view.getFragment() instanceof FourFragmentContract.View) {
                        fragmenArray[3] = view.getFragment();
                    } else if (view.getFragment() instanceof FiveFragmentContract.View) {
                        fragmenArray[4] = view.getFragment();
                    }
                }
            }
            fragments = Arrays.asList(fragmenArray);
        }
        return this.fragments;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

    public static OverallSituationComponent getOverallSituation(){
        return build;
    }

}
